/**
 * Revisão: Skubs em novembro de 2008 
 */

// Global object to hold resize information.
var resObj = new Object();

function resStart(event, id) {
  	var el;
  	var x, y;
  	
	var div = createAuxiliaryDiv();
	resObj.divNode = div;

  	// If an element id was given, find it. Otherwise use the element being
  	// clicked on.
  	if (id) {
    	resObj.elNode = document.getElementById('block_' + id);
  	} else {
    	if (browser.isIE)
      		resObj.elNode = window.event.srcElement;
    	if (browser.isNS)
      		resObj.elNode = event.target;
	    // If this is a text node, use its parent element.
	    if (resObj.elNode.nodeType == 3)
	      	resObj.elNode = resObj.elNode.parentNode;
	}

  	// Get cursor position with respect to the page.
  	if (browser.isIE) {
    	x = window.event.clientX + document.documentElement.scrollLeft
      		+ document.body.scrollLeft;
    	y = window.event.clientY + document.documentElement.scrollTop
      		+ document.body.scrollTop;
  	}
  	if (browser.isNS) {
    	x = event.clientX + window.scrollX;
    	y = event.clientY + window.scrollY;
  	}

  	// Save starting positions of cursor and element.
  	resObj.cursorStartX = x;
  	resObj.cursorStartY = y;
  	resObj.elStartLeft  = parseInt(resObj.elNode.style.left, 10);
  	resObj.elStartTop   = parseInt(resObj.elNode.style.top,  10);
  	resObj.elStartWidth  = parseInt(resObj.elNode.style.width, 10);
  	resObj.elStartHeight  = parseInt(resObj.elNode.style.height,  10);

  	if (isNaN(resObj.elStartLeft)) resObj.elStartLeft = 0;
  	if (isNaN(resObj.elStartTop))  resObj.elStartTop  = 0;

  	// Update element's z-index.
  	resObj.elNode.style.zIndex = ++globalZIndex;
  	resObj.divNode.style.zIndex = ++globalZIndex; 

	document.body.appendChild(resObj.divNode);

  	// Capture mousemove and mouseup events on the page.
  	if (browser.isIE) {
    	document.attachEvent("onmousemove", resGo);
	    document.attachEvent("onmouseup",   resStop);
	    window.event.cancelBubble = true;
	    window.event.returnValue = false;
  	}
  
  	if (browser.isNS) {
    	document.addEventListener("mousemove", resGo,   true);
	    document.addEventListener("mouseup",   resStop, true);
	    event.preventDefault();
  	}
}

function resGo(event) {
	var WIDTH_MIN = 470;
	var HEIGHT_MIN = 55;
	
  	var x, y;
  	var threshold = 5;
  	var newX, newY, oldX, oldY, oldH, oldW;
  	var delta;
  	var id = resObj.elNode.id.replace('block_', '');
  	var div = document.getElementById('block_content_' + id);

  	// Get cursor position with respect to the page.
  	if (browser.isIE) {
    	x = window.event.clientX + document.documentElement.scrollLeft
      		+ document.body.scrollLeft;
    	y = window.event.clientY + document.documentElement.scrollTop
      		+ document.body.scrollTop;
  	}
  	if (browser.isNS) {
    	x = event.clientX + window.scrollX;
    	y = event.clientY + window.scrollY;
 	}

  	// Move drag element by the same amount the cursor has moved.
  	newX = (resObj.elStartLeft + resObj.elStartWidth + x - resObj.cursorStartX);
  	newY = (resObj.elStartTop + resObj.elStartHeight  + y - resObj.cursorStartY);
  	
  	oldX = parseInt(resObj.elNode.style.left.replace('px',''), 10) + parseInt(resObj.elNode.style.width.replace('px',''), 10);
  	oldY = parseInt(resObj.elNode.style.top.replace('px',''), 10) + parseInt(resObj.elNode.style.height.replace('px',''), 10);
  	
  	oldH = parseInt(resObj.elNode.style.height.replace('px',''), 10);
  	oldW = parseInt(resObj.elNode.style.width.replace('px',''), 10);

	/* 
  	oldDivH = parseInt(div.style.height.replace('px',''), 10);
	if (isNaN(oldDivH)) {
		oldDivH = parseInt(div.style.height.replace('%',''), 10);
	}
	*/

  	if(Math.abs(oldX - newX) >= threshold) {
  		delta = Math.abs(oldX - newX);
		delta = delta / threshold;
		delta = Math.floor(delta) * threshold;
	
    	if(newX > oldX) {
	  		resObj.elNode.style.width = (oldW + delta) + "px";
		} else if ((oldW - delta) >= WIDTH_MIN){
	  		resObj.elNode.style.width = (oldW - delta) + "px";	  		
		}
	}

  	if(Math.abs(oldY - newY) >= threshold) {
  
  		delta = Math.abs(oldY - newY);
		delta = delta / threshold;
		delta = Math.floor(delta) * threshold;
	  
		if(newY > oldY)	{
	  		resObj.elNode.style.height  = (oldH + delta) + "px";
	  		//div.style.height = (oldDivH + delta) + "px";
		} else if ((oldH - delta) >= HEIGHT_MIN){
	  		resObj.elNode.style.height = (oldH - delta) + "px";
	  		//div.style.height = (oldDivH - delta) + "px";
		}
  	}

  	if (browser.isIE) {
    	window.event.cancelBubble = true;
    	window.event.returnValue = false;
  	}

  	if (browser.isNS) {
    	event.preventDefault();
    }
}

function resStop(event) {

    // Stop capturing mousemove and mouseup events.
	var el = resObj.elNode;
	var block_id = el.id.replace('block_', '');
	var block_w = el.style.width.replace('px','');
	var block_h = el.style.height.replace('px','');	
	document.body.removeChild(resObj.divNode);
	delete(resObj.divNode);
	
	/**
	 * SKUBS AJAX
	 */
//	AjaxFunctions.updateSize(block_id, block_w, block_h, null);

    if (browser.isIE) {
    	document.detachEvent("onmousemove", resGo);
    	document.detachEvent("onmouseup",   resStop);
    }
  
  	if (browser.isNS) {
    	document.removeEventListener("mousemove", resGo,   true);
    	document.removeEventListener("mouseup",   resStop, true);
    }
}